#!/bin/bash -x

edk2_repo_url="https://gitee.com/src-anolis-os/edk2.git"
edk2_repo_branch="a23"
edk2_repo_commit="47be5c088018a7747a94c4ea7884f187a81e9dbe"
edk2_repo_dir="/dev/shm/openanolis-edk2"

edk2_source_tree_builder_container_name="anolisos-23"
edk2_source_tree_builder_image_tag="openanolis/anolisos:23"
edk2_source_tree_builder_script="build-kata-ovmf-edk2-source-tree.sh"
edk2_source_tree_basename="edk2-edk2-stable202402"
edk2_source_tree_dir="/dev/shm/${edk2_source_tree_basename}"
edk2_source_tree_tarball="${edk2_source_tree_dir}.tgz"

if [ ! -e ${edk2_repo_dir} ]; then
  git clone ${edk2_repo_url} \
    -b ${edk2_repo_branch} \
    ${edk2_repo_dir}
fi
pushd ${edk2_repo_dir}
git reset --hard ${edk2_repo_commit}
popd # ${edk2_repo_dir}

generate_edk2_source_tree () {
  echo "[Generate edk2 source tree]"

  shared_top_dir=$(dirname ${edk2_source_tree_dir})

  if [ "X${https_proxy}" = "X" ]; then
    https_proxy=${HTTPS_PROXY}
  fi
  cat <<EOF | tee ${shared_top_dir}/${edk2_source_tree_builder_script}
#!/bin/bash -x

echo 'proxy=${https_proxy}' >> /etc/yum.conf
sync

stage=\${1:-1}

if [ \$stage -eq 1 ]; then
  yum install -y rpm-build dnf-plugins-core git \
    yum-utils

  echo '%_sourcedir ${edk2_repo_dir}' > ~/.rpmmacros

  pushd ${edk2_repo_dir}
  yum-builddep -y edk2.spec
  popd # ${edk2_repo_dir}
fi

if [ \$stage -eq 2 ]; then
  pushd ${edk2_repo_dir}
  rm -rf ~/rpmbuild || true
  rpmbuild -bp edk2.spec
  cd ~/rpmbuild/BUILD
  tar -zcf ${edk2_source_tree_tarball} ${edk2_source_tree_basename}
  popd # ${edk2_repo_dir}
fi
EOF
  chmod +x ${shared_top_dir}/${edk2_source_tree_builder_script}

  docker image pull ${edk2_source_tree_builder_image_tag}

  if [ $(docker ps -a | \
         grep -c "${edk2_source_tree_builder_container_name}") -eq 0 ]; then
    docker run \
      --name=${edk2_source_tree_builder_container_name} \
      -d \
      -v ${shared_top_dir}:${shared_top_dir} \
      ${edk2_source_tree_builder_image_tag} \
      sleep infinity
  else
    docker start ${edk2_source_tree_builder_container_name} || true
  fi
  docker exec -t ${edk2_source_tree_builder_container_name} \
    bash -c "${shared_top_dir}/${edk2_source_tree_builder_script} 1"
  docker exec -t ${edk2_source_tree_builder_container_name} \
    bash -c "${shared_top_dir}/${edk2_source_tree_builder_script} 2"
}

generate_edk2_source_tree

install_ovmf_deps () {
  echo "[Install ovmf dependencies]"

  sudo yum install -y make gcc gcc-c++ \
    python3 libuuid-devel \
    acpica-tools nasm
}

install_ovmf_deps

build_install_ovmf () {
  echo "[Build and Install kata ovmf]"
  tar -zxf ${edk2_source_tree_tarball} -C $(dirname ${edk2_source_tree_dir})

  pushd ${edk2_source_tree_dir}
  export PYTHON_COMMAND=$(which python3)
  make -C BaseTools/
  source edksetup.sh
  touch OvmfPkg/AmdSev/Grub/grub.efi
  build -b RELEASE -t GCC5 -a X64 -p OvmfPkg/AmdSev/AmdSevX64.dsc -n $(($(nproc) - 2))
  sudo mkdir -p /opt/kata/share/ovmf || true
  sudo cp Build/AmdSev/*/FV/OVMF.fd /opt/kata/share/ovmf/OVMFCSV.fd
  popd # ${edk2_source_tree_dir}
}

build_install_ovmf
